//
//  HomeController.m
//  SleepingElf
//
//  Created by karong on 2023/10/9.
//

#import "HomeController.h"
#import "HomeRequest.h"
#import "HomeModel.h"
#import "HomeBannerCell.h"
#import "HomeTabCell.h"
#import "HomeFeaturedCell.h"
#import "HomeClassifyCell.h"
#import "HomeClassifyMusicCell.h"
#import "HomeLullabyCell.h"
#import "HomeMusicCell.h"
#import "HomeHeaderView.h"
#import "HomeFooterView.h"
#import "HomeMoreController.h"
#import "MoreMusicController.h"

@interface HomeController () <HomeClassifyCellDelegate, HomeFeaturedCellDelegate, HomeLullabyCellDelegate, HomeHeaderViewDelegate, HomeFooterViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) HomeModel * homeModel;
@end

@implementation HomeController
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.hidesBottomBarWhenPushed = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self beginRefresh];
    self.titleLabel.text = WSLocalized(@"考拉睡眠監測");
    
            
}

- (void)refreshData {
    [SEToast loading];
    [HomeRequest loadHomeListWithSuccess:^(id obj) {
        [self loadSuccessWithArray:@[] isRefresh:YES];
        [SEToast dismiss];
        self.homeModel = obj;
        [self.listView reloadData];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        [self loadFailedWithError:error];
    }];
}
#pragma mark UICollectionDelegte
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.homeModel?7:0;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.homeModel rowWithSection:section];
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BaseCollectionViewCell * cell;
    NSString * cellClass = [self.homeModel cellClassWithSection:indexPath.section];
    if ([cellClass isEqualToString:@"HomeBannerCell"]) {
        HomeBannerCell * homeBannerCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeBannerCell" forIndexPath:indexPath];
//        homeBannerCell.delegate = self;
        [homeBannerCell setDataWithObject:self.homeModel.bannerArray];
        cell = homeBannerCell;
    }
    else if ([cellClass isEqualToString:@"HomeTabCell"]) {
        HomeTabCell * homeTabCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeTabCell" forIndexPath:indexPath];
        [homeTabCell setDataWithObject:self.homeModel.tabArray[indexPath.row]];
        cell = homeTabCell;
    }
    else if ([cellClass isEqualToString:@"HomeFeaturedCell"]) {
        HomeFeaturedCell * HomeFeaturedCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFeaturedCell" forIndexPath:indexPath];
        HomeFeaturedCell.delegate = self;
        [HomeFeaturedCell setDataWithObject:self.homeModel.featuredArray];
        cell = HomeFeaturedCell;
    }
    else if ([cellClass isEqualToString:@"HomeClassifyCell"]) {
        HomeClassifyCell * homeClassifyCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeClassifyCell" forIndexPath:indexPath];
        homeClassifyCell.delegate = self;
        [homeClassifyCell setDataWithObject:self.homeModel.classifyArray];
        cell = homeClassifyCell;
    }
    else if ([cellClass isEqualToString:@"HomeClassifyMusicCell"]) {
        HomeClassifyMusicCell * homeClassifyMusicCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeClassifyMusicCell" forIndexPath:indexPath];
        [homeClassifyMusicCell setDataWithObject:self.homeModel.classifyListArray[indexPath.row]];
        cell = homeClassifyMusicCell;
    }
    else if ([cellClass isEqualToString:@"HomeLullabyCell"]) {
        HomeLullabyCell * homeLullabyCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeLullabyCell" forIndexPath:indexPath];
        homeLullabyCell.delegate = self;
        [homeLullabyCell setDataWithObject:self.homeModel.lullabyArray];
        cell = homeLullabyCell;
    }
    else if ([cellClass isEqualToString:@"HomeMusicCell"]) {
        HomeMusicCell * homeMusicCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeMusicCell" forIndexPath:indexPath];
        [homeMusicCell setDataWithObject:self.homeModel.exclusiveArray[indexPath.row]];
        cell = homeMusicCell;
    }
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        HomeHeaderView * headerView;
        if (!headerView) {
            headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HomeHeaderView" forIndexPath:indexPath];
            headerView.delegate = self;
            [headerView setDataWithTitle:[self.homeModel headTitleWithSection:indexPath.section] showMore:YES];
            headerView.tag = [self.homeModel category_idWithSection:indexPath.section];
        }
        return headerView;
    }
    else if([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        HomeFooterView * footerView;
        if (!footerView) {
            footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HomeFooterView" forIndexPath:indexPath];
            footerView.delegate = self;
            footerView.tag = [self.homeModel category_idWithSection:4];
        }
        return footerView;
    }
    return nil;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return [self.homeModel sizeWithSection:indexPath.section];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return [self.homeModel headSizeWithSection:section];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    CGSize size = CGSizeZero;
    CGFloat width = SEScreenWidth;
    if (section == 4) {
        size.width = width;
        size.height = 52;
    }
    return size;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return [self.homeModel insetWithSection:section];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    id object = [self.homeModel didSelectWithIndexPath:indexPath];
    if (!object) {
        return;
    }
    if ([object isKindOfClass:NSClassFromString(@"MusicModel")]) {
        [[PlayerManager shared]playWithMusic:object];
        [self.tabBarController setSelectedIndex:2];
    }
    else {
        HomeTab * tab = object;
        //1:小憩2：专属睡眠
        if (tab.type == 1) {
            UIViewController * target = [self.storyboard instantiateViewControllerWithIdentifier:@"XiaoQiController"];
            [self.navigationController pushViewController:target animated:YES];
        }
        else if (tab.type == 2) {
            HomeMoreController * target = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeMoreController"];
            target.category_id = [self.homeModel category_idWithSection:4];
            [self.navigationController pushViewController:target animated:YES];
        }
    }
}

- (void)loadHomePlayClasslistWithClassify:(HomeClassify *)object {
    [HomeRequest loadHomePlayClasslistWithClassify_id:object.id page:1 pageSize:3 success:^(NSArray *array) {
        self.homeModel.classifyListArray = array;
        NSIndexSet *set =[[NSIndexSet alloc]initWithIndex:4];
        [self.listView reloadSections:set];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

#pragma mark - HomeClassifyCellDelegate
- (void)homeClassifyCellDidSelectItem:(HomeClassify *)object {
    [self loadHomePlayClasslistWithClassify:object];
}

#pragma mark - HomeFeaturedCellDelegate
- (void)homeFeaturedCellDidSelectWithObject:(MusicModel *)object {
    if ([[PlayerManager shared]musicIsPlaying:object]) {
        [[PlayerManager shared]pause];
    }
    else {
        [[PlayerManager shared]playWithMusic:object];
    }
}

#pragma mark - HomeLullabyCellDelegate
- (void)homeLullabyCellDidSelectWithObject:(id)object {
    if ([[PlayerManager shared]musicIsPlaying:object]) {
        [[PlayerManager shared]pause];
    }
    else {
        [[PlayerManager shared]playWithMusic:object];
    }
}

#pragma mark - HomeHeaderViewDelegate
- (void)homeHeaderViewMoreAction:(HomeHeaderView *)headerView {
    MoreMusicController * target = [self.storyboard instantiateViewControllerWithIdentifier:@"MoreMusicController"];
    target.classify_id = headerView.tag;
    [self.navigationController pushViewController:target animated:YES];
}

#pragma mark - HomeFooterViewDelegate
- (void)homeFooterViewMoreAction:(HomeFooterView *)footerView {
    HomeMoreController * target = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeMoreController"];
    target.category_id = footerView.tag;
    [self.navigationController pushViewController:target animated:YES];
}

@end
